package com.max.xihu.patterns.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: Max
 * @Date: 2020-09-13 22:30
 * @Description:
 */
public class CGlibClient {

    public static void main(String[] args) {
        ProductService service = new ProductService();

        ProductService enhancer = (ProductService) Enhancer.create(service.getClass(), new MethodInterceptor() {
                    @Override
                    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                        Object returnValue = "success";
                        Long param = (Long) objects[0];
                        if ("getProduct".equals(method.getName())){
                            returnValue = method.invoke(service,param);
                        }
                        return returnValue;
                    }
                });
        enhancer.getProduct(5L);
    }
}
